<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="手机号" align="center" prop="driverMobile" />
      <el-table-column label="总收益" align="center" prop="totalAmount" />
      <el-table-column label="可用收益" align="center" prop="avaAmount" />
      <el-table-column label="冻结收益" align="center" prop="freeAmount" />
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleYajin(scope.row)"
            v-hasPermi="['driver:account:editDeposit']"
          >押金</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleBill(scope.row)"
            v-hasPermi="['driver:bill:list']"
          >账单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 修改押金 -->
    <el-dialog :title="title" :visible.sync="open"  width="800px">

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="押金" prop="deposit">
          <el-input-number v-model="form.deposit"  :step="1" :min="0" :max="5000"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitYajin">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 账单 -->
    <el-dialog :title="title" :visible.sync="openBill" width="1100px" v-dialogdrag>
      <driver-bill :driverId="driverId" :key="(new Date().getTime())"></driver-bill>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBill">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, addAccount, updateAccount, exportAccount,updateYajin } from "@/api/driver/account";
import driverBill from "./driverBill";
export default {
  name: "Account",
  components: {driverBill},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 代驾司机账户表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openBill: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverId: undefined,
        driverName: undefined,
        idCard: undefined,
        mobile: undefined,
        totalAmount: undefined,
        avaAmount: undefined,
        freeAmount: undefined,
        gmtTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        driverId: [
          { required: true, message: "代价司机id不能为空", trigger: "blur" }
        ],
      },
      deposit: 0,
      driverId:null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代驾司机账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 取消按钮
    cancelBill() {
      this.openBill = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        driverId: undefined,
        driverName: undefined,
        idCard: undefined,
        totalAmount: undefined,
        avaAmount: undefined,
        freeAmount: undefined,
        deposit: 0,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代驾司机账户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代驾司机账户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleBill(row) {
      this.reset();
      this.driverId =  row.driverId;
      this.openBill = true;
      this.title = "代驾司机账单";
    },
    /** 押金按钮操作 */
    handleYajin(row) {
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代驾司机押金";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有代驾司机账户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAccount(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    submitYajin() {
      let  _this = this;
      this.$confirm('是否确认重置该代驾司机押金?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        return updateYajin(_this.form)

        }).then(() => {
        this.open = false;
        this.getList();
        this.msgSuccess("修改成功");
        }).catch(function() {
          console.log('yichange')
      });
    }
  }
};
</script>
